// Package api 提供HTTP路由和请求处理功能
// 该包使用Gin框架来处理HTTP请求和响应
package api

import (
	"net/http"

	"github.com/gin-gonic/gin"
)

// Response 统一响应结构体
type Response struct {
	Code    int         `json:"code"`
	Success bool        `json:"success"`
	Message string      `json:"message"`
	Data    interface{} `json:"data,omitempty"`
}

// ErrorCode 错误码常量定义
const (
	// SuccessCode 成功
	SuccessCode = 1

	// BadRequestCode 参数错误
	BadRequestCode = 10001

	// UnauthorizedCode 未授权
	UnauthorizedCode = 10002

	// ForbiddenCode 禁止访问
	ForbiddenCode = 10003

	// NotFoundCode 资源未找到
	NotFoundCode = 10004

	// ConflictCode 资源冲突
	ConflictCode = 10005

	// InternalServerErrorCode 服务器内部错误
	InternalServerErrorCode = 10006

	// ServiceUnavailableCode 服务不可用
	ServiceUnavailableCode = 10007
)

// 默认错误消息
const (
	DefaultBadRequestMessage         = "参数错误"
	DefaultUnauthorizedMessage       = "用户未登录"
	DefaultForbiddenMessage          = "权限不足"
	DefaultNotFoundMessage           = "资源未找到"
	DefaultConflictMessage           = "资源冲突"
	DefaultInternalServerMessage     = "服务器内部错误"
	DefaultServiceUnavailableMessage = "服务不可用"
)

// Success 返回成功响应
func Success(c *gin.Context, data interface{}) {
	c.JSON(http.StatusOK, Response{
		Code:    SuccessCode,
		Success: true,
		Message: "success",
		Data:    data,
	})
}

// SuccessWithMessage 返回带消息的成功响应
func SuccessWithMessage(c *gin.Context, message string, data interface{}) {
	c.JSON(http.StatusOK, Response{
		Code:    SuccessCode,
		Success: true,
		Message: message,
		Data:    data,
	})
}

// BadRequest 返回参数错误响应
func BadRequest(c *gin.Context, message ...string) {
	msg := DefaultBadRequestMessage
	if len(message) > 0 && message[0] != "" {
		msg = message[0]
	}
	c.JSON(http.StatusBadRequest, Response{
		Code:    BadRequestCode,
		Success: false,
		Message: msg,
	})
}

// Unauthorized 返回未授权错误响应
func Unauthorized(c *gin.Context, message ...string) {
	msg := DefaultUnauthorizedMessage
	if len(message) > 0 && message[0] != "" {
		msg = message[0]
	}
	c.JSON(http.StatusUnauthorized, Response{
		Code:    UnauthorizedCode,
		Success: false,
		Message: msg,
	})
}

// Forbidden 返回禁止访问错误响应
func Forbidden(c *gin.Context, message ...string) {
	msg := DefaultForbiddenMessage
	if len(message) > 0 && message[0] != "" {
		msg = message[0]
	}
	c.JSON(http.StatusForbidden, Response{
		Code:    ForbiddenCode,
		Success: false,
		Message: msg,
	})
}

// NotFound 返回资源未找到错误响应
func NotFound(c *gin.Context, message ...string) {
	msg := DefaultNotFoundMessage
	if len(message) > 0 && message[0] != "" {
		msg = message[0]
	}
	c.JSON(http.StatusNotFound, Response{
		Code:    NotFoundCode,
		Success: false,
		Message: msg,
	})
}

// Conflict 返回资源冲突错误响应
func Conflict(c *gin.Context, message ...string) {
	msg := DefaultConflictMessage
	if len(message) > 0 && message[0] != "" {
		msg = message[0]
	}
	c.JSON(http.StatusConflict, Response{
		Code:    ConflictCode,
		Success: false,
		Message: msg,
	})
}

// InternalServerError 返回服务器内部错误响应
func InternalServerError(c *gin.Context, message ...string) {
	msg := DefaultInternalServerMessage
	if len(message) > 0 && message[0] != "" {
		msg = message[0]
	}
	c.JSON(http.StatusInternalServerError, Response{
		Code:    InternalServerErrorCode,
		Success: false,
		Message: msg,
	})
}

// ServiceUnavailable 返回服务不可用错误响应
func ServiceUnavailable(c *gin.Context, message ...string) {
	msg := DefaultServiceUnavailableMessage
	if len(message) > 0 && message[0] != "" {
		msg = message[0]
	}
	c.JSON(http.StatusServiceUnavailable, Response{
		Code:    ServiceUnavailableCode,
		Success: false,
		Message: msg,
	})
}
